#include "AspectInstaller.h"
#include <stdio.h>

IAspectManager* gAspectManager = CAspectInstaller::Instance();

CAspectInstaller::CAspectInstaller()
{
}

CAspectInstaller* CAspectInstaller::Instance()
{
	return &instance;
}

void CAspectInstaller::InstallAdvice(void *_pObject)
{
	CObject* pObject = reinterpret_cast<CObject*>(_pObject);
	Vtlb* vtlb = pObject->pVtbl;
	this->objects.insert(std::make_pair(pObject,vtlb));
	pObject->pVtbl = &aspectInstallerVtlb;
}

void __stdcall CAspectInstaller::Invoke(CObject* pObject)
{
	CAspectInstaller* pThis = CAspectInstaller::Instance();
	Vtlb* vtlb = pThis->objects[pObject];
	std::list<IMethodInterceptorAdvice*>::iterator pIt = pThis->aspects.begin();
	while ( pIt!=pThis->aspects.end() )
	{
		(*pIt)->Before();
		pIt++;
	}

	// Perform invocation of the target method
	vtlb->Func00(pObject);

	pIt = pThis->aspects.begin();
	while ( pIt!=pThis->aspects.end() )
	{
		(*pIt)->After();
		pIt++;
	}
}

CAspectInstaller CAspectInstaller::instance;
Vtlb CAspectInstaller::aspectInstallerVtlb(CAspectInstaller::Invoke);

Vtlb::Vtlb(void (__stdcall *Func00)(CObject* pObject))
{
	this->Func00 = Func00;
}

